package cn.dapeng.designPatterns.singleton;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * 这种实现方式锁的粒度太大
 */
public class LazySingleton_01 {

    private static LazySingleton_01 instance;

    private LazySingleton_01() {
    }

    public synchronized static LazySingleton_01 getInstance() {
        if (instance == null) {
            instance = new LazySingleton_01();
        }
        return instance;
    }

    public static void main(String[] args) {
        Set<Integer> set = new CopyOnWriteArraySet<>();
        int testTimes = 10_0000;

        for (int i = 0; i < testTimes; i++) {
            new Thread(() -> {
                set.add(LazySingleton_01.getInstance().hashCode());
            }).start();
        }

        System.out.println("size:: " + set.size());
    }
}
